﻿using System;
using NUnit.Framework;

namespace SeleniumToolkit.Util
{
    [TestFixture]
    public class RegistryKeyHelperFixture
    {
        string _originalInstallDir;

        [TestFixtureSetUp]
        public void FixtureSetup()
        {
            var helper = new RegistryKeyHelper(@"SOFTWARE\SeleniumToolkit");
            if (helper.RegistryKeyExists)
            {
                _originalInstallDir = helper.GetValue("InstallDir");
            }
        }

        [TestFixtureTearDown]
        public void FixtureTearDown()
        {
            if (!String.IsNullOrEmpty(_originalInstallDir))
            {
                var helper = new RegistryKeyHelper(@"SOFTWARE\SeleniumToolkit", true);
                // Environment.GetFolderPath value changes based on CPU Target, 
                //  ie Any CPU = C:\Program Files
                //  x86 = C:\Program Files (x86)\
                //  x64 = C:\Program Files\
                //string path = Path.Combine(@"C:\Program Files (x86)\", "Selenium Toolkit");
                //string path = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Selenium Toolkit");
                //helper.SetValue("InstallDir", path);
                helper.SetValue("InstallDir", _originalInstallDir);
            }
        }

        [Test]
        public void CanCreateKey()
        {
            var helper = new RegistryKeyHelper(@"SOFTWARE\SeleniumToolkit", true);

            Assert.IsTrue(helper.RegistryKeyExists);

            helper.SetValue("InstallDir", @"C:\");

            Assert.AreEqual(@"C:\", helper.GetValue("InstallDir"));
        }

        [Explicit("Run Nunit.exe or nunit-console.exe on 64 bit machine.")]
        [Test]
        public void CanGetKeyAs64BitProcess()
        {
            var helper = new RegistryKeyHelper(@"SOFTWARE\SeleniumToolkit");
            Assert.AreEqual(@"SOFTWARE\Wow6432Node\SeleniumToolkit", helper.KeyName, "Path should have been redirected.");
        }

        [Explicit("Run Nunit-x86.exe or nunit-console-x86.exe")]
        [Test]
        public void CanGetKeyAs32BitProcess()
        {
            var helper = new RegistryKeyHelper(@"SOFTWARE\SeleniumToolkit");
            Assert.AreEqual(@"SOFTWARE\SeleniumToolkit", helper.KeyName, "Path should not have been redirected.");
        }

        [Test]
        public void CanDeleteKey()
        {
            var helper = new RegistryKeyHelper(@"SOFTWARE\SeleniumToolkit", true);

            Assert.IsTrue(helper.RegistryKeyExists);

            helper.Delete();

            helper = new RegistryKeyHelper(@"SOFTWARE\SeleniumToolkit");
            Assert.IsFalse(helper.RegistryKeyExists);
        }
    }
}
